package com.base.andlangcore.util

import android.util.Log
import com.base.andlangcore.BuildConfig

/**
 * 日志打印工具
 */
class LogUtils {
    companion object{
        private const val TAG:String="LogUtils"
        //类名
        private var className: String? = null

        //方法名
        private var methodName: String? = null

        //行数
        private var lineNumber = 0

        private fun createLog(log: String): String {
            val buffer = StringBuffer()
            buffer.append(methodName)
            buffer.append("(").append(className).append(":").append(lineNumber)
                .append("):")
            buffer.append(log)
            return buffer.toString()
        }

        /**
         *
         * 获取文件名、方法名、所在行数
         *
         *
         *
         * @param sElements
         */
        private fun getMethodNames(sElements: Array<StackTraceElement>) {
            className = sElements[1].fileName
            methodName = sElements[1].methodName
            lineNumber = sElements[1].lineNumber
        }

        //log.d
        fun d(text: String) {
            if (BuildConfig.DEBUG) {
                if (!BaseLangUtils.isEmpty(text)) {
                    getMethodNames(Throwable().stackTrace)
                    Log.d(TAG, createLog(text))
                }
            }
        }

        //log.i
        fun i(text: String) {
            if (BuildConfig.DEBUG) {
                if (!BaseLangUtils.isEmpty(text)) {
                    getMethodNames(Throwable().stackTrace)
                    Log.i(TAG, createLog(text))
                }
            }
        }

        //log.e
        fun e(text: String) {
            if (BuildConfig.DEBUG) {
                if (!BaseLangUtils.isEmpty(text)) {
                    getMethodNames(Throwable().stackTrace)
                    Log.e(TAG, createLog(text))
                }
            }
        }
    }
}